/*  File Name	: DirectoryMonitor.h
 *  Author		: Pang Jiacai
 *	 E-mail		: 823960168@qq.com
 *  Created on	: 2012-10-30
 *  Description:
 */

#ifndef DirectoryMONITOR_H_
#define DirectoryMONITOR_H_
#include "Monitor.h"
#include "MonitorSock.h"
#include "DirectoryService.h"
#include "Configurator.h"
#include "Timer.h"

struct Room
{
	Room()
	{
		reset();
	}
	void reset(void)
	{
		room_id = 0;
		member = 0;
		limit_member =0;
		min_chips=0;
		max_chips = 0;
		is_register=false;
	}
	int room_id;
	int member;//当前房间人数
	int limit_member;//房间人数上限
	int min_chips;//最小筹码
	int max_chips;//最大筹码
	bool is_register;
};
typedef std::map<int,Room> RoomMap;

struct GateWay
{
	GateWay()
	{
		reset();
	}
	void reset(void)
	{
		game_type = 0;
		server_id=0;
		gate_id = 0;
		port =0;
		total_player = 0;
		ip ="";
		game_name="";
		ds = NULL;
		room_map.clear();
	}
	int game_type;
	int server_id;
	int gate_id;
	int port;
	int total_player;
	DirectoryService *ds;
	std::string game_name;
	std::string ip;
	RoomMap room_map;
};
typedef std::map<int,GateWay*> GateWayMap;

struct GameInfo
{
	GameInfo()
	{
		type = 0;
		total_player = 0;
		name = "";
		svr_map.clear();
	}
	int type;
	int total_player;
	std::string name;
	GateWayMap svr_map;
};
typedef std::vector<GameInfo> GameVec;

class DirectoryMonitor:public Monitor<DirectoryService>
{
public:
	typedef Monitor<DirectoryService> super;
	typedef MonitorAcceptor<DirectoryMonitor,DirectoryService> DircAcceptor;
	typedef MonitorConnector<DirectoryMonitor,DirectoryService> DircConnector;
	typedef std::map<std::string,DirectoryService*> WebMap;

	struct RoutineTimer: public Timer
	{
		RoutineTimer()
		{
			monitor = NULL;
		}
		virtual int handle_timeout(void);
		DirectoryMonitor *monitor;
	};

	virtual ~DirectoryMonitor();

	static DirectoryMonitor *instance(void);

	virtual int init(int line_id,int list_size=10240,int block_size=4096,int pool_size=10240,
			int growth_size=1024,std::string pool_name="DirectoryMonitor-Msg");
	virtual int start(void);
	virtual int stop(void);

	virtual int run_request_loop(void);
	virtual int run_respond_loop(void);

	const Json::Value &cfg(void);

	virtual int handle_close(DirectoryService *ds);
	void collection_info(void);

	int respond_succ_to_client(int id,int cmd,MessageBlock *mb=NULL);
	int respond_error_to_client(int id,int cmd,int error,MessageBlock *mb=NULL);

private:
	DirectoryMonitor();

	int gate_login(DirectoryService *ds,MessageBlock *mb);
	int gate_logout(DirectoryService *ds,MessageBlock *mb);
	int gate_update(DirectoryService *ds,MessageBlock *mb);
	int client_get_list(DirectoryService *ds,MessageBlock *mb);
	int make_up_room_info(int type,MessageBlock *mb);

	std::string key(std::string ip,int port);
	int register_to_web(void);
	int make_up_game_info(MessageBlock *mb);
	DirectoryService* find_web(std::string ip,int port);
	DirectoryService* new_web_conn(std::string ip,int port);


	int process_request(MessageBlock *mb);
	int process_respond(MessageBlock *mb);

	static DirectoryMonitor* instance_;
	DircAcceptor *acceptor_;
	DircConnector *connector_;

	GameVec game_vec_;
	RoutineTimer routine_timer_;

	WebMap web_map_;

	RT_MUTEX rt_mutex_;
};

#endif /* DATASYNCMONITOR_H_ */
